package mockup;

import java.util.ArrayList;

import data.Appointment;
import data.Data;
import data.Invite;
import data.Meeting;
import data.Message;
import data.User;
import data.room.Reservation;
import data.room.Room;

/**
 * Creates mockup objects from 'real' objects
 * @author Erik
 *
 */
public class MockupFactory {

	/**
	 * Returns a mockup corresponding to the supplied data object.
	 * The proper class of the data object is used when creating the mockup. 
	 * @param data The data object to get a mockup for
	 * @return A mockup object corresponding to the data object
	 */
	public static Mockup getMockup(Data data) {
		if(data instanceof Meeting)
			return getMeetingMockup((Meeting)data);
		else if(data instanceof Appointment)
			return getAppointmentMockup((Appointment)data);
		else if(data instanceof Room)
			return getRoomMockup((Room)data);
		else if(data instanceof User)
			return getUserMockup((User)data);
		else if(data instanceof Invite)
			return getInviteMockup((Invite)data);
		else if(data instanceof Message)
			return getMessageMockup((Message)data);
		
		System.err.println("The class " + data.getClass().getName() + " is unsupported in MockupFactory");
		return null;
	}

	/**
	 * Gets an empty mockup instance for the supplied data class.
	 * @param dataClass The class to get an instance for
	 * @return The method returns an instance where every field is either 0 or null.
	 */
	@SuppressWarnings("rawtypes")
	public static Mockup getEmptyMockup(Class dataClass) {
			
		if(dataClass == Meeting.class)
			return new MeetingMockup(0, null, null, null, 0, null, null);
		else if(dataClass == Appointment.class)
			return new AppointmentMockup(0, null, null, null, 0, null);
		else if(dataClass == Room.class)
			return new RoomMockup(0, null, null, null, null, 0);
		else if(dataClass == User.class)
			return new UserMockup(0, null, 0, null, null, null);
		else if(dataClass == Invite.class)
			return new InviteMockup(0, 0);
		else if(dataClass == Message.class)
			return new MessageMockup(0, 0);
		
		System.err.println("The class " + dataClass.getClass().getName() + " is unsupported in MockupFactory");
		return null;
	}
	
	/**
	 * Returns a mockup for the data object
	 * @param object The object to convert to a mockup
	 * @return The mockup
	 */
	public static MessageMockup getMessageMockup(Message object) {
		return new MessageMockup(object.getId(), object.getMeeting().getId(), 
				object.getReceiver().getId(), object.getSubject(), object.getDescription(), 
				object.getSendTime().getTimeInMillis(), object.getReadTime().getTimeInMillis());
	}

	/**
	 * Returns a mockup for the data object
	 * @param object The object to convert to a mockup
	 * @return The mockup
	 */
	public static InviteMockup getInviteMockup(Invite object) {
		InviteMockup invite = new InviteMockup(object.getId(), object.getMeeting().getId());
		invite.setAnswer(object.getAnswer());
		invite.setDescription(object.getDescription());
		invite.setReadTime(object.getReadTime().getTimeInMillis());
		invite.setSendTime(object.getSendTime().getTimeInMillis());
		invite.setReceiver(object.getReceiver().getId());
		invite.setSubject(object.getSubject());
		return invite;
	}

	/**
	 * Returns a mockup for the data object
	 * @param object The object to convert to a mockup
	 * @return The mockup
	 */
	public static UserMockup getUserMockup(User object) {
		
		return new UserMockup(object.getId(), object.getName(), object.getDateOfBirth().getTimeInMillis(), 
				object.getEmail(), object.getPhone(), object.getDepartment());
	}

	/**
	 * Returns a mockup for the data object
	 * @param object The object to convert to a mockup
	 * @return The mockup
	 */
	public static RoomMockup getRoomMockup(Room object) {
		return new RoomMockup(object.getId(), object.getName(), object.getLocation(), object.getCasualName(),
				object.getDescription(), object.getCapacity());
	}
	
	/**
	 * Returns a mockup for the data object
	 * @param object The object to convert to a mockup
	 * @return The mockup
	 */
	public static AppointmentMockup getAppointmentMockup(Appointment object) {
		ReservationMockup res;
		if(object.getReservation() == null)
			res = null;
		else
			res = getReservationMockup(object.getReservation());
		AppointmentMockup app = new AppointmentMockup(object.getId(), object.getTitle(), object.getDescription(), 
				object.getLocation(), object.getOwner().getId(), res );
		
		if(res != null)
			res.setAppointment(app);
		
		return app;
		
	}

	/**
	 * Returns a mockup for the data object
	 * @param object The object to convert to a mockup
	 * @return The mockup
	 */
	public static ReservationMockup getReservationMockup(Reservation res) {
		int roomId;
		if(res.getRoom() == null)
			roomId = -1;
		else
			roomId = res.getRoom().getId();
		
		return new ReservationMockup(res.getId(),res.getStartTime().getTimeInMillis(), res.getEndTime().getTimeInMillis(), roomId);
	}

	/**
	 * Returns a mockup for the data object
	 * @param object The object to convert to a mockup
	 * @return The mockup
	 */
	public static MeetingMockup getMeetingMockup(Meeting object) {
		
		ArrayList<Integer> participants = new ArrayList<Integer>();
		for(User user : object.getParticipants()) {
			participants.add(user.getId());
		}
		
		ReservationMockup res;
		if(object.getReservation() == null)
			res = null;
		else
			res = getReservationMockup(object.getReservation());

		MeetingMockup meet = new MeetingMockup(object.getId(), object.getTitle(), object.getDescription(), 
				object.getLocation(), object.getOwner().getId(), res, participants);
		if(res != null)
			res.setAppointment(meet);
		
		return meet;
	}
}
